<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $guarded = [];

    public static function createOrderNumber()
    {
        do {
            $number  = mt_rand(1000, 9999).date('YmdHis').sprintf('%08d', auth()->id());
            $isEmpty = self::where('order_number', $number)
                ->limit(1)
                ->get()
                ->isEmpty();
        } while (!$isEmpty);

        return $number;
    }

    public function good()
    {
        return $this->belongsTo(Good::class, 'good_id', 'id');
    }
}
